package com.rainbow.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rainbow.common.util.ResultUtil;
import com.rainbow.common.vo.Result;
import com.rainbow.modules.sys.entity.Dict;
import com.rainbow.modules.sys.entity.DictData;
import com.rainbow.modules.sys.service.DictDataService;
import com.rainbow.modules.sys.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Auth rainbow
 */
@Slf4j
@RestController
@Api(description = "字典管理接口")
@RequestMapping("/rainbow/dict")
@Transactional
public class DictController {

    @Autowired
    private DictService dictService;

    @Autowired
    private DictDataService dictDataService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping(value = "/getAll")
    @ApiOperation(value = "获取全部数据")
    public Result<List<Dict>> getAll() {

        List<Dict> list = dictService.list();
        return new ResultUtil<List<Dict>>().setData(list);
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "添加")
    public Result<Object> add(@ModelAttribute Dict dict) {

        if (dictService.getOne(new QueryWrapper<Dict>().eq("type", dict.getType())) != null) {
            return new ResultUtil<Object>().setErrorMsg("字典类型Type已存在");
        }
        dictService.save(dict);
        return new ResultUtil<Object>().setSuccessMsg("添加成功");
    }

    @PostMapping(value = "/edit")
    @ApiOperation(value = "编辑")
    public Result<Object> edit(@ModelAttribute Dict dict) {

        Dict old = dictService.getById(dict.getId());
        // 若type修改判断唯一
        if (!old.getType().equals(dict.getType()) && dictService.getOne(new QueryWrapper<Dict>().eq("type", dict.getType())) != null) {
            return new ResultUtil<Object>().setErrorMsg("字典类型Type已存在");
        }
        dictService.updateById(dict);
        return new ResultUtil<Object>().setSuccessMsg("编辑成功");
    }

    @RequestMapping(value = "/delByIds/{id}", method = RequestMethod.DELETE)
    @ApiOperation(value = "通过id删除")
    public Result<Object> delAllByIds(@PathVariable String id) {


        Dict dict = dictService.getById(id);
        dictService.removeById(id);
        dictDataService.remove(new QueryWrapper<DictData>().eq("dict_id", id));
        // 删除缓存
        redisTemplate.delete("dictData::" + dict.getType());
        return new ResultUtil<Object>().setSuccessMsg("删除成功");
    }

    @GetMapping(value = "/search")
    @ApiOperation(value = "搜索字典")
    public Result<List<Dict>> searchPermissionList(@RequestParam String key) {

        List<Dict> list = dictService.list(new QueryWrapper<Dict>().
                like("title", key).or().like("type", key).or().like("description", key));
        return new ResultUtil<List<Dict>>().setData(list);
    }
}
